//
//  IWAccountTool.m
//  WeiBo17
//
//  Created by teacher on 15/8/19.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "IWAccountTool.h"
#import "IWAccount.h"


#define ACCOUNT_PATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"]

@implementation IWAccountTool


+ (void)saveAccount:(IWAccount *)account{
    [NSKeyedArchiver archiveRootObject:account toFile:ACCOUNT_PATH];
}


+ (IWAccount *)account{
    //在这个方法里面去判断是否有帐号以及帐号是否过期
    IWAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:ACCOUNT_PATH];
    
    if (!account) {
        return nil;
    }
    //判断一下是否过期.
    //求出当前帐号过期的时间
    NSDate* date = [account.createDate dateByAddingTimeInterval:account.expires_in];
    //拿着过期的时间与当前的时间进行对比
    NSDate* currentDate = [NSDate date];
    
    //    timeIntervalSinceDate -->可以直接计算出两个时间的差值(秒)
    //    compare               -->可以对比出两个时间的早晚
    
    if ([currentDate compare:date] != NSOrderedAscending) {
        //代表过期
        account = nil;
    }
    return account;
}


@end
